package com.example.controller;

import com.example.entity.Admin;
import com.example.entity.Dormitory;
import com.example.mapper.AdminMapper;
import com.example.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Admin")
public class AdminConstoller {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminService adminService;
    @GetMapping
    public List<Admin> index(){
        List<Admin> findall = adminMapper.findAll();
        return findall;
    }
    @PostMapping
    public Map<String, Object> saveAdmin(@RequestBody Admin admin) {
        return adminService.saveAdmin(admin);
    }

    @DeleteMapping("/{admin_num}")
    public Map<String, Object> deleteAdmin(@PathVariable Integer admin_num) {
        return adminService.deleteAdmin(admin_num);
    }

    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam String name) {
        pageNum = (pageNum - 1) * pageSize;
        List<Admin> data = adminMapper.selectPage(pageNum, pageSize,name);
        Integer total = adminMapper.selectTotal(name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }


}
